package com.punai.dataanalysis.epdatakb.service.NG001;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.exception.ServiceException;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.bo.NG001.JtczLqClf;
import com.punai.dataanalysis.epdatakb.mapper.JtczLqClfMapper;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.system.mapper.EcoCompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class NG001JtczLqClfService {

    @Autowired
    private JtczLqClfMapper lqClfMapper;
    @Autowired
    private EcoCompanyMapper ecoCompanyMapper;

    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        Map<String, Object> yearMonth = dataAnalysisVo.getYearMonth();
        List<JtczLqClf> resultList = new ArrayList<>();

        List<JtczResultBo> wccz = lqClfMapper.wccz(yearMonth);
        Map<String, BigDecimal> wcczMap = wccz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> zc = lqClfMapper.zc(yearMonth);
        Map<String, BigDecimal> zcMap = zc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> fc = lqClfMapper.fc(yearMonth);
        Map<String, BigDecimal> fcMap = fc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> mb = lqClfMapper.mb(yearMonth);
        Map<String, BigDecimal> mbMap = mb.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        List<JtczResultBo> zczlf = lqClfMapper.zczlf(yearMonth);
        Map<String, BigDecimal> zczlfMap = zczlf.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal));
        /**
         * 专业 - 路桥 公司
         */
        List<EcoCompany> ecoCompanies = ecoCompanyMapper.selectList(Wrappers.<EcoCompany>lambdaQuery().eq(EcoCompany::getType, "Z")
                .eq(EcoCompany::getSource, "L")
                .eq(EcoCompany::getStatus,"0")
        );

        if (CollUtil.isEmpty(ecoCompanies)) {
            throw new ServiceException("无统计单位!");
        }

        List<String> companyNames = ecoCompanies.stream().map(EcoCompany::getCompanyName).distinct().sorted().collect(Collectors.toList());

        for (String companyName : companyNames) {
            JtczLqClf lqClf = new JtczLqClf();
            lqClf.setDeptName(companyName);
            lqClf.setBnwccz(wcczMap.getOrDefault(companyName,BigDecimal.ZERO));
            lqClf.setZc(zcMap.getOrDefault(companyName,BigDecimal.ZERO));
            lqClf.setFc(fcMap.getOrDefault(companyName,BigDecimal.ZERO));
            lqClf.setMb(mbMap.getOrDefault(companyName,BigDecimal.ZERO));
            lqClf.setZzzl(zczlfMap.getOrDefault(companyName,BigDecimal.ZERO));
            lqClf.setClfhj(NumberUtil.adds(
                    lqClf.getZc(),
                    lqClf.getFc(),
                    lqClf.getMb(),
                    lqClf.getZzzl()
            ));
            calcZb(lqClf);
            resultList.add(lqClf);
        }
        calcPm(resultList);
        JtczLqClf xj = new JtczLqClf();
        xj.setDeptName("路桥单位小计");
        calcXj(resultList,xj);
        resultList.add(xj);
        return resultList;
    }

    private void calcPm(List<JtczLqClf> resultList) {
        resultList.sort(Comparator.comparing(JtczLqClf::getZczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczLqClf lqClf = resultList.get(i);
            lqClf.setZcpm(i + 1);
        }
        resultList.sort(Comparator.comparing(JtczLqClf::getFczb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczLqClf lqClf = resultList.get(i);
            lqClf.setFcpm(i + 1);
        }
        resultList.sort(Comparator.comparing(JtczLqClf::getMbzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczLqClf lqClf = resultList.get(i);
            lqClf.setMbpm(i + 1);
        }
        resultList.sort(Comparator.comparing(JtczLqClf::getZzzlzb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczLqClf lqClf = resultList.get(i);
            lqClf.setZzzlpm(i + 1);
        }
        resultList.sort(Comparator.comparing(JtczLqClf::getZb,Comparator.reverseOrder()));
        for (int i = 0; i < resultList.size(); i++) {
            JtczLqClf lqClf = resultList.get(i);
            lqClf.setPm(i + 1);
        }
    }

    private void calcZb(JtczLqClf lqClf) {
        BigDecimal bnwccz = lqClf.getBnwccz();
        BigDecimal zc = lqClf.getZc();
        BigDecimal fc = lqClf.getFc();
        BigDecimal mb = lqClf.getMb();
        BigDecimal zzzl = lqClf.getZzzl();
        BigDecimal clfhj = lqClf.getClfhj();
        lqClf.setZczb(NumberUtil.divideUseProportion(zc,2,bnwccz));
        lqClf.setFczb(NumberUtil.divideUseProportion(fc,2,bnwccz));
        lqClf.setMbzb(NumberUtil.divideUseProportion(mb,2,bnwccz));
        lqClf.setZzzlzb(NumberUtil.divideUseProportion(zzzl,2,bnwccz));
        lqClf.setZb(NumberUtil.divideUseProportion(clfhj,2,bnwccz));
    }

    private void calcXj(List<JtczLqClf> resultList,JtczLqClf xj) {
        BigDecimal bnwccz = BigDecimal.ZERO;
        BigDecimal zc     = BigDecimal.ZERO;
        BigDecimal fc     = BigDecimal.ZERO;
        BigDecimal mb     = BigDecimal.ZERO;
        BigDecimal zzzl   = BigDecimal.ZERO;
        BigDecimal clfhj  = BigDecimal.ZERO;

        for (JtczLqClf lqClf : resultList) {
            bnwccz  = NumberUtil.add(bnwccz ,lqClf.getBnwccz());
            zc      = NumberUtil.add(zc     ,lqClf.getZc());
            fc      = NumberUtil.add(fc     ,lqClf.getFc());
            mb      = NumberUtil.add(mb     ,lqClf.getMb());
            zzzl    = NumberUtil.add(zzzl   ,lqClf.getZzzl());
            clfhj   = NumberUtil.add(clfhj  ,lqClf.getClfhj());
        }

        xj.setBnwccz( bnwccz  );
        xj.setZc(     zc      );
        xj.setFc(     fc      );
        xj.setMb(     mb      );
        xj.setZzzl(   zzzl    );
        xj.setClfhj(  clfhj   );

        calcZb(xj);
    }

}
